package controller;

import java.io.BufferedReader;
import java.io.File;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.List;
import java.util.UUID;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import com.google.gson.Gson;
import com.mybatis.mapper.CheckinfoMapper;
import com.mybatis.po.Checkinfo;

import service.CheckService;
import util.MySessionContext;

@CrossOrigin(originPatterns="*",allowCredentials="true")
@Controller
public class Check {

	   
	@Autowired

	private CheckService checkservice;
  @RequestMapping("check")
  @ResponseBody
  public String regUpload1(List<MultipartFile> photo,HttpServletRequest request,HttpServletResponse response,
		  String sid,String cid) throws IOException {
	  int ck;
	  String ans="";
	  String tmpimg="";//图片保存的路径
	  System.out.println(sid);
	  //跨域导致servlet无法接收sessionid，这里让前端发送请求时一并发送，因为先版本的HttpServlet不支持那个可以通过sessionid来找到
	  //session的方法了，所以这里通过利用监听器来来根据sessionid找到session（具体内部怎么实现还没研究）
	  MySessionContext myc= MySessionContext.getInstance();
	  //myc.delSession(request.getSession());
	  HttpSession session = myc.getSession(sid);
	  System.out.println(session);
	  if(session!=null) {
			String user=String.valueOf(session.getAttribute("Uid"));
			System.out.println(user);
		}
	  if(session.getAttribute("tmpnum")==null) session.setAttribute("tmpnum", 0);//tmpnum属性代表前端第几次请求
	  int num=Integer.parseInt(session.getAttribute("tmpnum").toString());
	  session.setAttribute("tmpnum", num+1);



	  tmpimg=checkservice.getimage(photo);
	  ans+=checkservice.sistance(tmpimg);
	  System.out.println(ans);
	  if(ans.equals("无")) ck=0;
	  else ck=1;
      if(num%2==0) {
    	  checkservice.add((String)session.getAttribute("Uid"),(String)session.getAttribute("Uname"),cid,ck);
      }
      return ans;
  }
}
